package com.demo.analyze;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;

public class Where {

    /**
     * [
     *  [
     *   ["name","wangyy"],
     *   ["or"],
     *   ["age",20]
     *  ],
     *  ["and"],
     *  [
     *   ["10",">",1],
     *   ["and"],
     *   ["32",">",20]
     *  ],
     *  ["or"],
     *  [["db","mysql"]]
     * ]
     *
     * select * from x
     * where ((name = 'wnagyy') or (age = 20)) and ((10 > 1) and (32 > 20)) or ((db = 'mysql'))
     *
     */
    public static void main(String[] args) throws IOException {
        String inputStr = "[[ [\"name\",\"wangyy\"], [\"or\"], [\"age\",20] ], [\"and\"], [[\"10\",\">\",1], [\"and\"], [\"32\",\">\",20] ], [\"or\"], [[\"db\",\"mysql\"]]]";

        ObjectMapper objectMapper = new ObjectMapper();
        Object input = objectMapper.readValue(inputStr, Object.class);

        String result = where(input);
        System.out.println("WHERE " + result);
    }

    public static String where(Object input) {
        StringBuilder sb = new StringBuilder();
        List<Object> list = (List<Object>) input;
        for (int i = 0; i < list.size(); i++) {
            List element = (List) list.get(i);
            //一维数组
            if (!isArrays(element)) {
                if (element.size() == 2) {
                    String field = (String) element.get(0);
                    Object value = element.get(1);
                    sb.append("(").append(field).append("=").append(value).append(")");
                } else if (element.size() == 1) {
                    String operator = (String) element.get(0);
                    if ("or".equalsIgnoreCase(operator)) {
                        sb.append(" or ");
                    } else if ("and".equalsIgnoreCase(operator)) {
                        sb.append(" and ");
                    }
                } else if (element.size() == 3) {
                    String field = (String) element.get(0);
                    String operator = (String) element.get(1);
                    Object value = element.get(2);
                    sb.append("(").append(field).append(operator).append(value).append(")");
                }
            } else {
                //多维数组
                String innerCondition = where(element);
                if (i == 0 || i == list.size() - 1) {
                    sb.append("(").append(innerCondition).append(")");
                } else {
                    sb.append(innerCondition);
                }
            }
        }
        return sb.toString();
    }


    /**
     * 是否是多维数组
     *
     * @param list
     * @return
     */
    public static boolean isArrays(Object list) {
        int dimension = 1;
        if (list instanceof List) {
            List<?> input = (List<Object>) list;
            while (!input.isEmpty() && input.get(0) instanceof List<?>) {
                dimension++;
                if (dimension > 1)
                    return true;
                input = (List<?>) input.get(0);
            }
        }
        return false;
    }

}
